/**
 * 广寒宫
 * 网址:www.guanghangong.xyz
 */
package org.moon.framework.autoconfigure.thread;

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.Executor;

/**
 * @Async 默认使用的线程池
 * @author moon
 */
@Configuration
public class TaskExecutorAutoConfiguration implements AsyncConfigurer {
	/**
	 * Set the ThreadPoolExecutor's core pool size.
	 */
	private static final int CORE_POOL_SIZE = 2;
	/**
	 * Set the ThreadPoolExecutor's maximum pool size.
	 */
	private static final int MAX_POOL_SIZE = 2;
	/**
	 * Set the capacity for the ThreadPoolExecutor's BlockingQueue.
	 */
	private static final int QUEUE_CAPACITY = 10;

	/**
	 * 通过重写getAsyncExecutor方法，制定默认的任务执行由该方法产生
	 * @Async 默认使用的线程池
	 * 配置类实现AsyncConfigurer接口并重写getAsyncExcutor方法，并返回一个ThreadPoolTaskExevutor
	 * 这样我们就获得了一个基于线程池的TaskExecutor
	 */
	@Override
	public Executor getAsyncExecutor() {
		ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
		taskExecutor.setCorePoolSize(CORE_POOL_SIZE);
		taskExecutor.setMaxPoolSize(MAX_POOL_SIZE);
		taskExecutor.setQueueCapacity(QUEUE_CAPACITY);
		taskExecutor.setThreadNamePrefix("moon-task-pool");
		taskExecutor.initialize();
		return taskExecutor;
	}
}